package l3dapp;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class LApp extends JFrame {
	//elements
	//menu bar
	private JMenuBar mainMenu;
	private JMenu fileMenu;
	private JMenuItem fileNew;
	private JMenuItem fileOpen;
	private JMenuItem fileSave;
	private JMenuItem fileExit;
	//tabs
	private JSplitPane split;
	private JScrollPane varTab;
	private JScrollPane actTab;
	
	//data
	private JPanel varList;
	private JPanel actList;
	private LinkedList<VarPanel> variables;
	
	//constructor
	public LApp(String s) {
		super(s);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (checkOut() == 0) {
					System.exit(0);
				}
			}
		});
		
		constructMenu();
		add(mainMenu,BorderLayout.NORTH);
		constructWorkArea();
		add(split,BorderLayout.CENTER);
		
		loadVariables();
		
		setBounds(100,100,500,500);
	}
	
	/* load variables */
	private void loadVariables() {
		variables = new LinkedList<VarPanel>();
		for (int i = 0; i < 20; i++) {
			variables.add(new VarPanel());
		}
		Iterator<VarPanel> i = variables.iterator();
		while (i.hasNext())
			varList.add(i.next());
		varTab.setViewportView(varList);
	}
	
	/* construct tabs */
	private void constructWorkArea() {
		//TODO: make pretty
		actList = new JPanel();
		varList = new JPanel();
		varList.setLayout(new BoxLayout(varList,BoxLayout.PAGE_AXIS));
		varTab = new JScrollPane(varList);
		actTab = new JScrollPane(actList);
		Dimension minimumSize = new Dimension(100, 50);
		varTab.setMinimumSize(minimumSize);
		actTab.setMinimumSize(minimumSize);
		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, varTab, actTab);
		split.setOneTouchExpandable(true);
		split.setDividerLocation(150);
	}
	
	/* construct menu bar */
	private void constructMenu() {
		//TODO: icons, descriptions
		mainMenu = new JMenuBar();
		
		fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		mainMenu.add(fileMenu);
		
		fileNew = new JMenuItem("New");
		fileNew.setMnemonic(KeyEvent.VK_N);
		fileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		fileMenu.add(fileNew);

		fileOpen = new JMenuItem("Open");
		fileOpen.setMnemonic(KeyEvent.VK_O);
		fileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		fileMenu.add(fileOpen);
		
		fileSave = new JMenuItem("Save");
		fileSave.setMnemonic(KeyEvent.VK_S);
		fileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		fileMenu.add(fileSave);
		
		fileExit = new JMenuItem("Exit");
		fileExit.setMnemonic(KeyEvent.VK_X);
		fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
		fileMenu.add(fileExit);
	}
	
	/* check if ok to close application, return 0 if file saved or No pressed */
	int checkOut() {
		//TODO: check if file needs saving, return 0 on permission to exit
		return 0;
	}
	
	//public methods
	public void startApp() {
		setVisible(true);
	}
	
}
